package com.zhfi.dataapply.schedule.companylist;

import com.zhfi.dataapply.mapper.common.CompanyIdListMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CompanyListServiceImpl implements CompanyListService, InitializingBean {

    private int size;
    private List<String> companyIdList;

    @Autowired
    private CompanyIdListMapper companyIdListMapper;

    @Override
    public String getCompanyId(int siteNum) {
        return companyIdList.get(siteNum);
    }

    @Override
    public int getSize() {
        return this.size;
    }

    @Override
    public void reset() {
        List<String> tempList = null;
        try {
            tempList = companyIdListMapper.selectAllCompanyId();
        } catch (Exception e) {
            log.error("获取企业列表失败，沿用前一天企业列表", e);
        }
        if (tempList != null) {
            this.companyIdList = companyIdListMapper.selectAllCompanyId();
            this.size = companyIdList.size();
        }
    }


    @Override
    public void afterPropertiesSet() {
        reset();
    }
}
